package com.ll.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import com.ll.bean.ManagerUserBean;
import com.ll.dao.DB;
import com.ll.util.FormatUtil;
import com.ll.util.MD5Security;

public class RegAction extends ActionSupport implements ModelDriven<ManagerUserBean>
{
	private static final long serialVersionUID = 1L;
	private ManagerUserBean user = new ManagerUserBean();
	
	public ManagerUserBean getUser()
	{
		return user;
	}
	public void setUser(ManagerUserBean user)
	{
		this.user = user;
	}
	
	public ManagerUserBean getModel()
	{
		return user;
	}
	
	@Override
	public String execute() throws Exception
	{
		ActionContext ctx=ActionContext.getContext();
		
		// 开始写数据库
		Connection con = null;
		try
		{
			con = DB.getConnection();
			
			String md5 = MD5Security.code(user.getUpwd());
			PreparedStatement st = 
					con.prepareStatement("INSERT INTO t_manager_user(username, upwd," +
							"company, uemail, phone, province," +
							" city, regist_datetime, active)" +
							" VALUES(?,?,?,?,?,?,?,?,?)");
			st.setString(1, user.getUsername());
			st.setString(2, md5);
			st.setString(3, user.getCompany());
			st.setString(4, user.getUemail());
			st.setString(5, user.getPhone());
			st.setString(6, user.getProvince());
			st.setString(7, user.getCity());
			st.setString(8, FormatUtil.date2String(new Date()));
			st.setString(9, "1");
			
			int re = st.executeUpdate();
			
			if(re == 1)
			{
				ctx.getSession().put("_manager_user", user);
				
				//跳转管理页
				return SUCCESS;
			}
			else
			{
				return "reg";
			}
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try {
				if(con != null)
					con.close();
				}catch(Exception e){
					e.printStackTrace();
				}
		}
		return ERROR;	
	}
}
